﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsApp1.Leo.week02
{
    //皮重列表
    public partial class TareShow : Form
    {
        public TareShow()
        {
            InitializeComponent();
            GetUPCData();
        }

        //商品代号
        public async void GetUPCData()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<UPCDataser>>>(URLCommon.Url2, "/WeekListOne_Leo/GetUPCDataShow", HttpClientHelper.MethodType.GET, default);

            comboBox1.DataSource = items.Data;
            comboBox1.DisplayMember = "UPCDataName";
            comboBox1.ValueMember = "GoodsMinuteId";
        }

        public class UPCDataser
        {
            public int GoodsMinuteId { get; set; }
            public string UPCDataName { get; set; }
        }

        public int pageIndex = 1;

        public int pageSize = 2;

        public int totalcount = 0;

        public int pagecount = 0;

        public int GoodsMinuteId = 0;


        public class Tareres
        {
            public int pageIndex { get; set; }

            public int pageSize { get; set; }

            public int GoodsMinuteId { get; set; }
        };

        /// <summary>
        /// 皮重列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void TareShow_Load(object sender, EventArgs e)
        {
            var clients = new Tareres
            {
                pageIndex = pageIndex,
                pageSize = pageSize,
                GoodsMinuteId = GoodsMinuteId,
            };

            var items = await HttpClientHelper.RequestUrl<object, ApiResultFY1<List<Tarees>>>(URLCommon.Url2, "/WeekListOne_Leo/GetTareShow", HttpClientHelper.MethodType.POST, clients);
            this.dataGridView1.DataSource = items.Data.list;
            totalcount = items.Data.totalcount;
            pagecount = items.Data.pagecount;
        }

        public class Tarees
        {
            /// <summary>
            /// 商品代号
            /// </summary>
            public string UPCDataName { get; set; }

            /// <summary>
            /// 计量单位
            /// </summary>
            public string TareMeasure { get; set; }

            /// <summary>
            /// 标准重量
            /// </summary>
            public string TareStandardWeight { get; set; }

            /// <summary>
            /// 使用状态
            /// </summary>
            public bool TareStatus { get; set; } = true;

            /// <summary>
            /// 备注
            /// </summary>
            public string TareRemark { get; set; }

            /// <summary>
            /// 是否禁用
            /// </summary>
            public bool IsDisable { get; set; }
        }


        //状态显示判断
        private void dataGridView1_CellFormatting_1(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["使用状态"].Index && e.Value != null)
            {
                if (e.Value.ToString() == "True")
                {
                    e.Value = "正在运行";
                    e.FormattingApplied = true;
                }
                else
                {
                    e.Value = "已停运";
                    e.FormattingApplied = false;
                }
            }

            if (e.ColumnIndex == dataGridView1.Columns["是否禁用"].Index && e.Value != null)
            {
                if (e.Value.ToString() == "True")
                {
                    e.Value = "未禁用";
                    e.FormattingApplied = true;
                }
                else
                {
                    e.Value = "已禁用";
                    e.FormattingApplied = false;
                }
            }
        }



        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text != null)
            {
                GoodsMinuteId = Convert.ToInt32(comboBox1.SelectedValue);
            }

            TareShow_Load(sender, e);
        }

        //首页
        private void button1_Click(object sender, EventArgs e)
        {
            if (pageIndex != 1)
            {
                pageIndex = 1;
            }
            TareShow_Load(sender, e);
        }

        //上一页
        private void button2_Click(object sender, EventArgs e)
        {
            if (pageIndex > 1)
            {
                pageIndex--;
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经第一页了");
            }
            TareShow_Load(sender, e);
        }

        //下一页
        private void button3_Click(object sender, EventArgs e)
        {
            if (pageIndex < pagecount)
            {
                pageIndex++;
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经最后一页了");
            }
            TareShow_Load(sender, e);
        }

        //尾页
        private void button4_Click(object sender, EventArgs e)
        {
            if (pageIndex != pagecount)
            {
                pageIndex = pagecount;
            }
            TareShow_Load(sender, e);
        }
    }
}
